﻿/// <reference path="sp.js" />
/// <reference path="sp.debug.js" />
'use strict';

ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");

var ANNOUNCEMENT_LIST_NAME = "New Announcements List";
var webSite,
    hostUrl,
    siteUrl,
    collList;

function initializePage() {

    $(document).ready(function () {
        hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
        siteUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    });

    function getQueryStringParameter(paramToRetrieve) {
        var params = document.URL.split("?")[1].split("&");
        var strParams = "";

        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split("=");
            if (singleParam[0] == paramToRetrieve)
                return singleParam[1];
        }
    }
}

function btnOperationOnClick() {
    switch ($('#optList').val()) {
        case "Get All Announcement Lists":
            getAnnouncementLists();
            break;
        case "Add a new Announcement List":
            createAnnouncementList(ANNOUNCEMENT_LIST_NAME);
            break;
        case "Update Announcement List":
            updateAnnouncementList(ANNOUNCEMENT_LIST_NAME);
            break;
        case "Delete First Announcement List":
            deleteAnnouncementList(ANNOUNCEMENT_LIST_NAME);
            break;
        default:

    }
}

function getAnnouncementLists() {

    $('#message').text("fetching announcement lists...");

    var currentContext = SP.ClientContext.get_current();
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);

    webSite = hostContext.get_web();
    collList = webSite.get_lists();

    currentContext.load(collList);//, 'Include(Title, Id)');
    currentContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

    function onQuerySucceeded() {
        var listInfo = '';
        var listEnumerator = collList.getEnumerator();

        while (listEnumerator.moveNext()) {
            var list = listEnumerator.get_current();
            var listTemplateType = list.get_baseTemplate();
            if (listTemplateType === SP.ListTemplateType.announcements) {
                listInfo += 'Title: ' + list.get_title() + " " + "<br/>";
                listInfo += 'Description: ' + list.get_description() + " " + "<br/><br/>";
            }
        }
        $('#message').html(listInfo);
    }

    function onQueryFailed(sender, args) {
        $('#message').text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
}

function createAnnouncementList(listName) {

    $('#message').text("creating announcement list...");

    var currentContext = SP.ClientContext.get_current();
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);

    webSite = hostContext.get_web();

    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(listName);
    listCreationInfo.set_templateType(SP.ListTemplateType.announcements);

    var list = webSite.get_lists().add(listCreationInfo);

    currentContext.load(list);
    currentContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

    function onQuerySucceeded() {
        var result = list.get_title() + ' created.';
        $('#message').text(result);
    }

    function onQueryFailed(sender, args) {
        $('#message').text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
}

function updateAnnouncementList(listName) {
    $('#message').text("updating announcement list...");

    var currentContext = SP.ClientContext.get_current();
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);

    webSite = hostContext.get_web();

    var list = webSite.get_lists().getByTitle(listName);
    list.set_description(listName + "---updated");
    list.update();

    currentContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

    function onQuerySucceeded() {
        var result = listName + ' updated.';
        $('#message').text(result);
    }

    function onQueryFailed(sender, args) {
        $('#message').text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

}

function deleteAnnouncementList(listName) {

    $('#message').text("deleting announcement list...");

    var currentContext = SP.ClientContext.get_current();
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);

    webSite = hostContext.get_web();

    var list = webSite.get_lists().getByTitle(listName);
    list.deleteObject();

    currentContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

    function onQuerySucceeded() {
        var result = listName + ' deleted.';
        $('#message').text(result);
    }

    function onQueryFailed(sender, args) {
        $('#message').text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
}